﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;

namespace VideoMedia
{
    public partial class ConsultFilm : Form
    {
        private List<Film> tousLesFilms = new List<Film>();
        private List<Realisateur> tousLesReal = new List<Realisateur>();
        private List<Acteur> tousLesAct = new List<Acteur>();
        public ConsultFilm()
        {
            InitializeComponent();
        }

        private void ConsultFilm_Load(object sender, EventArgs e)
        {
            XmlSerializer xs = new XmlSerializer(typeof(List<Film>));
            StreamReader sr = new StreamReader("film.xml");
            List<Film> Lf = xs.Deserialize(sr) as List<Film>;
            sr.Close();
            tousLesFilms = Lf;
            dataGridView1.DataSource = Lf;
            dataGridView1.Columns[3].Visible = false;
            dataGridView1.Columns[4].Visible = false;
            dataGridView1.Columns[5].Visible = false;
            chargerReal();
            chargerAct();
            foreach (Realisateur r in tousLesReal)
            {
                if (r.NomReal != "")
                {
                    cbRealSearch.Items.Add(r.NomReal);
                }
            }
            foreach(Acteur a in tousLesAct)
            {
                if (a.NomActeur != null)
                {
                    cbActSearch.Items.Add(a.NomActeur);
                }
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void EcrireFilm()
        {
            XmlSerializer xs = new XmlSerializer(typeof(List<Film>));
            StreamWriter sr = new StreamWriter("film.xml");
            xs.Serialize(sr, tousLesFilms);
            sr.Close();
        }

        private void chargerReal()
        {
            XmlSerializer xs = new XmlSerializer(typeof(List<Realisateur>));
            StreamReader sr = new StreamReader("realisateur.xml");
            List<Realisateur> Lr = xs.Deserialize(sr) as List<Realisateur>;
            tousLesReal = Lr;
            sr.Close();
        }
        private void chargerAct()
        {
            XmlSerializer xs = new XmlSerializer(typeof(List<Acteur>));
            StreamReader sr = new StreamReader("acteur.xml");
            List<Acteur> La = xs.Deserialize(sr) as List<Acteur>;
            tousLesAct = La;
            sr.Close();
        }

        private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int ligneActu = e.RowIndex;
                DataGridViewRow ligne = dataGridView1.Rows[ligneActu];
                String valeurCellule = ligne.Cells[3].Value.ToString();
                pictureBox1.ImageLocation = valeurCellule;
                String valeurSyno = ligne.Cells[6].Value.ToString();
                lbSyno.Text = valeurSyno;

            }
            catch { }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            ComboBox cb =(ComboBox) sender;
            if (cb.Text == "(tous les genre)")
            {
                dataGridView1.DataSource = tousLesFilms;
            }
            else
            {
                List<Film> parGenre = new List<Film>();
                foreach (Film f in tousLesFilms)
                {
                    foreach (string ge in f.Genre)
                    {
                        if (ge ==cb.Text)
                        {
                            parGenre.Add(f);

                        }
                    }
                }
                
                dataGridView1.DataSource = parGenre;
            }
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int ligneActu = e.RowIndex;
                DataGridViewRow ligne = dataGridView1.Rows[ligneActu];
                String lienVideo = ligne.Cells[5].Value.ToString();
                Form1 f1 = new Form1(lienVideo);
                f1.ShowDialog();
                f1.Close();
            }
            catch { 
            }
        }


        private void tbSearch_TextChanged(object sender, EventArgs e)
        {
            if (tbSearch.Text == "")
            {
                dataGridView1.DataSource = tousLesFilms;
            }
            else
            {
                List<Film> rechFilm = new List<Film>();
                foreach (Film f in tousLesFilms)
                {
                    if (f.NomFilm.ToUpper().Contains(tbSearch.Text.ToUpper()))
                    {
                        rechFilm.Add(f);
                    }
                }
                dataGridView1.DataSource = rechFilm;
            }
        }

        private void cbRealSearch_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<Film> rechFilm = new List<Film>();
            foreach (Film f in tousLesFilms)
            {
                foreach (Realisateur r in f.RealsFilm)
                {
                    if (r.NomReal == cbRealSearch.Text)
                    {
                        rechFilm.Add(f);
 
                    }
                }
            }
            dataGridView1.DataSource = rechFilm;
        }

        private void cbActSearch_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<Film> rechFilm = new List<Film>();
            foreach (Film f in tousLesFilms)
            {
                foreach (Acteur a in f.ActsFilm)
                {
                    if (a.NomActeur == cbActSearch.Text)
                    {
                        rechFilm.Add(f);
                    }
                }
            }
            dataGridView1.DataSource = rechFilm;

        }

        private void btRemoveMovie_Click(object sender, EventArgs e)
        {
                int i = dataGridView1.CurrentCell.RowIndex;
                string ligneNom = (string)dataGridView1.Rows[i].Cells[0].Value;
                string lignereals = (string)dataGridView1.Rows[i].Cells[1].Value;
                foreach (Film f in tousLesFilms)
                {
                    if (f.NomFilm == ligneNom && f.Realisateurs == lignereals)
                    {
                        tousLesFilms.Remove(f);
                        break;
                    }
                }
                dataGridView1.Refresh();
                EcrireFilm();
            
        }

        private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
             
        }
    }
}
 